package com.quanshu.change.view;

import android.content.IntentFilter;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.change.broadcast.PayResultBroadcatReciver;
import com.quanshu.change.person.view.GoldListActivity;
import com.quanshu.change.person.view.GoldRechargeActivity;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.entity.AccountBalanceEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoldActivity extends BaseActivity implements PayResultBroadcatReciver.PayResultSuccessfulListener {

    @BindView(R.id.title)
    BaseTitleBar title;
    @BindView(R.id.line_center)
    View lineCenter;
    @BindView(R.id.gold_bean)
    ImageView goldBean;
    @BindView(R.id.gold_bean_txt)
    TextView goldBeanTxt;
    @BindView(R.id.gold_bean_txt1)
    TextView goldBeanTxt1;
    @BindView(R.id.view_title)
    ConstraintLayout viewTitle;
    @BindView(R.id.cl1)
    ConstraintLayout cl1;
    @BindView(R.id.cl2)
    ConstraintLayout cl2;
    @BindView(R.id.cl3)
    ConstraintLayout cl3;
    private double money = 0;
    private PayResultBroadcatReciver payResultBroadcatReciver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gold);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBalance();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
        unregisterReceiver(payResultBroadcatReciver);
    }

    private void initView() {
        ImmersionBar.with(this).statusBarColor("#FFFFFF").titleBar(title).statusBarDarkFont(true).init();
        payResultBroadcatReciver = new PayResultBroadcatReciver(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PayResultBroadcatReciver.PAY_GOLD_RECHAGR_SUCCESSFUL);
        registerReceiver(payResultBroadcatReciver, intentFilter);
        title.setCenterText("金豆").setRightText("金豆明细").setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {
                BaseTool.goActivity(GoldActivity.this, GoldListActivity.class, null);
            }
        });
        //跳转分享金豆
        RxViewHelper.getInstance().clicks(cl1, aVoid -> {
            if (money < 100) {
                ToastHelper.toast("余额不足100，暂不能分享");
                return;
            }
            BaseTool.goActivity(GoldActivity.this, GoldShareActivity.class, null);
        });
        //跳转购买金豆
        RxViewHelper.getInstance().clicks(cl2, aVoid -> {
            BaseTool.goActivity(GoldActivity.this, GoldRechargeActivity.class, null);
        });
        //跳转提现金豆
        RxViewHelper.getInstance().clicks(cl3, aVoid -> {
            if (money < 100) {
                ToastHelper.toast("余额不足100，暂不能提现");
                return;
            }
            BaseTool.goActivity(GoldActivity.this, GoldWithdrawActivity.class, null);
        });
        getBalance();
    }


    //获取账户余额
    private void getBalance() {
        if (currentUser != null) {
            NetClient.getInstance().getShopService(ShopInterface.class)
                    .getBalance(currentUser.getId())
                    .compose(new BaseTransformer<>(this, true, false))
                    .subscribe(new BaseObserver<BaseResponse<AccountBalanceEntity>>() {
                        @Override
                        public void onSuccess(BaseResponse<AccountBalanceEntity> response) {
                            if (response.getCode() == 1) {
                                currentUser.setScore(response.getData().getScore());
                                currentUser.setGp(response.getData().getGP());
                                MyApplication.getInstance().setCurrentUser(currentUser);
                                money = response.getData().getGold_bean();
                                goldBeanTxt1.setText(String.valueOf(response.getData().getGold_bean()));

                            }
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            LogUtil.printException(e);
                        }
                    });
        }
    }

    @Override
    public void paySuccess() {
        getBalance();
    }

}
